package com.zht.dao;

import com.zht.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {


    /* 封装一个登陆方法*/
    public  boolean login(String name,String pwd){
        /*
        *
        *
        * */
        Connection  connection = null;
        PreparedStatement preparedStatement= null;
       try {
           // 注册驱动
           Class.forName("com.mysql.jdbc.Driver");
           // 2. 获取
           connection = JDBCUtils.getConnection();
           // 3. 定义sql
           String sql = "select * from user where name = ? and password = ?";
           // 4. 得到执行SQL 对象
           preparedStatement = connection.prepareStatement(sql);
            // 5. 给？ 这个占位符赋值
           preparedStatement.setString(1,name);
           preparedStatement.setString(2,pwd);
           // 6.利用执行sql 对象，执行sql
           ResultSet resultSet = preparedStatement.executeQuery();
           return  resultSet.next();
       }catch (Exception e){
           e.printStackTrace();
       }finally {
            // 7 .关闭资源
           JDBCUtils.close(connection,preparedStatement);
       }
       return false;
    }



}
